"""
@Project:newpro
@File:websocket_anli.py
@Author:毛敬龙
@Date:9:55
"""

import tornado.websocket
conndict={}
class WebsockHandler(tornado.websocket.WebSocketHandler):
    # 跨域支持
    def check_origin(self, origin):
        return True
    #建立连接
    def open(self):
        #获取参数
        id = int(self.get_query_argument("id"))
        #存储链接
        conndict[id]=self
    #接收消息
    def on_message(self,mes):
        print(mes)
        # self.write_message(mes)

    def on_close(self):
        pass

import tornado.web
class TTHandler(tornado.web.RequestHandler):
    def get(self):
        msg = self.get_query_argument("mes")
        id = int(self.get_query_argument("id"))
        conndict[id].write_message(msg)
        # 根据id返回对应客户信息，其价值和意义

import time
from utils.my_redis import r
from init_db import dbs
class HouseHandler(tornado.web.RequestHandler):
    def get(self):
        id=2
        while True:
            key1=r.get_str('house_1')
            if key1:
                key1 = int(key1.decode())
                sql='select * from greenhouse where id=4'
                res=dbs.findone(sql)
                status=1
                if key1<int(res['heat_min']) or key1>int(res['heat_max']):
                    status=2
                conndict[id].write_message({'code':200,'msg':'返回','heat':key1,'status':status})
            time.sleep(10)


        # while True:   # 失败，无法实时获取最新信息
        #     time.sleep(15)
        #     sql = 'select * from greenhouse'
        #     res = dbs.find_all(sql)
        #     lis = []
        #     for i in res:
        #         if int(i['heat_min'])<int(i['heat'])<int(i['heat_max']):
        #             status=1
        #         else:
        #             status=2
        #         dat={
        #             'id':i['id'],
        #             'name':i['name'],
        #             'heat':i['heat'],
        #             'status':status
        #         }
        #         lis.append(dat)
        #     conndict[id].write_message({'code':200,'msg':'成功','data':lis})
        #     print(1)